<%@page
	import="com.gadglet.core.RequestException,com.gadglet.data.*,com.gadglet.data.utils.*,com.gadglet.params.*,javax.jdo.PersistenceManager,com.google.appengine.api.users.*,com.gadglet.data.utils.PMF,com.gadglet.util.*,com.gadglet.util.UrlUtils,java.util.*,javax.jdo.Query,java.util.logging.Logger"%>
	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="/static/pageStyle.css"
	title="pageStyle" media="screen,projection" />
<title>Gadglet v0.3  - Registration: Add Domain</title>
</head>
<body>
<div id="content">
<p>
<%
boolean userChangedId = false;
Logger log = Logger.getLogger("addDomain");
if(request.getParameter("createDomain")== null ||  request.getParameter("createDomain").isEmpty())
{%>
<%@include file="/secure/messages/createDomain.jsp"%>
<%
}
else
{
	UserService userService = UserServiceFactory.getUserService();
	User user = userService.getCurrentUser();
	String thisURL = request.getRequestURI();
	String domainName=null;
	ServletContext context = null; 
	String registrationMethod = null;
	int maxActiveUsers = 10;
	
	try
	{
		context  = getServletContext();
		registrationMethod = context.getInitParameter(SharedConstants.ctxParamRegMethod);
		domainName = context.getInitParameter(SharedConstants.ctxParamServingDomain);
		maxActiveUsers = Integer.parseInt(context.getInitParameter(SharedConstants.ctxParamMaxActiveUsersForDomain));
	}
	catch (Exception e){
		log.warning("failed to read context");
	}
	if (user != null) 
	{
		/*check if a valid Google apps gomain
		*https://www.google.com/a/abc.com/o8/ud?be=o8
		*/
		String googleAppsBaseAuthDomain = "https://www.google.com/a/";
		// Gmail or other providers 
		if(!user.getAuthDomain().startsWith(googleAppsBaseAuthDomain) || user.getAuthDomain().length()< googleAppsBaseAuthDomain.length() )
				response.sendRedirect(UrlUtils.getHomeUrl(request, true, false)
					+ "/secure/logoutRegister.jsp");
		
		// restricted domain NOT multi tenant
		if(!domainName.equalsIgnoreCase("*") && !user.getAuthDomain().startsWith(googleAppsBaseAuthDomain+"/a/"+domainName) )
			response.sendRedirect(UrlUtils.getHomeUrl(request, true, false)
					+ "/secure/logoutRegister.jsp");
	

		String timezone = (String) session.getAttribute("timezone");
		
		String token  = (String) session.getAttribute(SharedConstants.registrationTokenParamName);
		
		RegistrationToken regToken = null;
		DomainAccount domainAccount = null;
		
		if(token!=null)
			regToken = RegistrationTokenUtils.getTokenById(token);
		// load token info
		if(regToken!=null)
		{	
			try{
				domainAccount = DomainAccountUtils.createNewDomainAndAdminUser(regToken,timezone,registrationMethod,maxActiveUsers);
				}
				catch ( RequestException e)
				{
					if(e.getCustomError().equals("userChangedIdOnRegistration"))
						userChangedId = true;
				}
			catch (Exception e){log.warning(e.getMessage());}	
		}
		if (domainAccount != null){ 
			%><%@include file="/secure/messages/registrationComplete.jsp"%><%
		}
		else if(userChangedId){
				%><%@include file="/secure/messages/notGoogleApps.jsp"%><%
		}
		else{%><%@include file="/secure/messages/registrationProblem.jsp"%><%}
	} 
	else 
	{
		response.sendRedirect(UrlUtils.getHomeUrl(request, true, false)
				+ "/secure/register.jsp");
	}
}
%>
</p>
</div>
</body>
</html>




